package com.sktlab.bizconfmobile.model.manager;

import java.util.ArrayList;
import java.util.Hashtable;

import android.database.Cursor;

import com.sktlab.bizconfmobile.model.Meeting;
import com.sktlab.bizconfmobile.model.db.MeetingDBAdapter;
import com.sktlab.bizconfmobile.model.db.MeetingDbTable;
import com.sktlab.bizconfmobile.util.CalendarUtil;
import com.sktlab.bizconfmobile.util.Util;

public class MeetingManager {
	
	public static final String TAG = "MeetingManager";
	
    private ArrayList<Meeting> mMeetings = null;
	private Hashtable<String, ArrayList<Meeting>> mDayToMeetings;
	private MeetingDBAdapter mMeetingDb;
	
	private MeetingManager() {
		
		mDayToMeetings = new Hashtable<String, ArrayList<Meeting>>();
		mMeetings = new ArrayList<Meeting>(); 
		mMeetingDb = new MeetingDBAdapter(MeetingDbTable.MEETING_DB_TABLE, 
				MeetingDbTable.getAllColumns());
	}
	
	private static class InstanceHolder {

		private static MeetingManager instance = new MeetingManager();
	}
	
	public static MeetingManager getInstance() {

		return InstanceHolder.instance;
	}
	
	public boolean hasRecord(Long date) {
		
		String dateFomat = CalendarUtil.getFomatDateStr(date);
		
		//Util.BIZ_CONF_DEBUG(TAG, "date fomat str: " + dateFomat);
		return mDayToMeetings.containsKey(dateFomat);
	}
	
	public void addMeeting(Meeting newMeeting) {
	
		mMeetings.add(newMeeting);
		
		String key = newMeeting.getDate();
		
		if(mDayToMeetings.containsKey(key)) {
			
			Util.BIZ_CONF_DEBUG(TAG, "contains key: " + key);
			
			mDayToMeetings.get(key).add(newMeeting);
		}else {
			
			Util.BIZ_CONF_DEBUG(TAG, "add new key: " + key);
			ArrayList<Meeting> dateMeeting = new ArrayList<Meeting>();
			dateMeeting.add(newMeeting);
			
			mDayToMeetings.put(key, dateMeeting);
		}
	}
	
	public ArrayList<Meeting> getCellMeetings(String dateFomatStr) {
		
		ArrayList<Meeting> meetings = null;
		
		if (mDayToMeetings.containsKey(dateFomatStr)) {
			
			meetings = mDayToMeetings.get(dateFomatStr);
		}
		
		return meetings;
	}
	
	public Hashtable<String, ArrayList<Meeting>> getDayToConfs() {
		return mDayToMeetings;
	}

	public void setDayToConfs(Hashtable<String, ArrayList<Meeting>> dayToConfs) {
		this.mDayToMeetings = dayToConfs;
	}

	public  ArrayList<Meeting> getAllMeetings() {
		return mMeetings;
	}

	public  void setMeetings(ArrayList<Meeting> confs) {
		mMeetings = confs;
	}	
	
	public void loadMeetingFromDb() {

		mMeetings.clear();
		mDayToMeetings.clear();		
		mMeetingDb.open();
			
		Cursor cursor = mMeetingDb.fetchAllObjects(null, null);	
		
		while (cursor.moveToNext()) {
			
			//Util.BIZ_CONF_DEBUG(TAG, "meeting has data");
			int id = cursor.getInt(cursor
					.getColumnIndex(MeetingDbTable.KEY_ID));
			
			String accountId = cursor.getString(cursor
					.getColumnIndex(MeetingDbTable.KEY_ACCOUNT_ID));
			
			String title = cursor.getString(cursor
					.getColumnIndex(MeetingDbTable.KEY_TITLE));
			
			String startDate = cursor.getString(cursor
					.getColumnIndex(MeetingDbTable.KEY_STARTDATE));
			
			String endDate = cursor.getString(cursor
					.getColumnIndex(MeetingDbTable.KEY_ENDDATE));
			
			String accessNumber = cursor.getString(cursor
					.getColumnIndex(MeetingDbTable.KEY_ACCESS_NUMBER));
			
			String notes = cursor.getString(cursor
					.getColumnIndex(MeetingDbTable.KEY_NOTES));
			
			String invitees = cursor.getString(cursor
					.getColumnIndex(MeetingDbTable.KEY_INVITEES));
			
			String date = cursor.getString(cursor
					.getColumnIndex(MeetingDbTable.KEY_MEETING_DATE));
			
			Meeting meeting = new Meeting();
			meeting.setAccountId(accountId);
			meeting.setTitle(title);
			meeting.setStartTime(Long.valueOf(startDate));
			meeting.setEndTime(Long.valueOf(endDate));
			meeting.setAccessNumber(accessNumber);
			meeting.setNote(notes);
			meeting.setInvitees(invitees);						
			meeting.setDate(date);
			
			mMeetings.add(meeting);
		
			if (mDayToMeetings.containsKey(date)) {
				
				//Util.BIZ_CONF_DEBUG(TAG, "add meeting,date: " + date);
				mDayToMeetings.get(date).add(meeting);
			} else {
				
				ArrayList<Meeting> dateMeetings = new ArrayList<Meeting>();
				dateMeetings.add(meeting);
				mDayToMeetings.put(date, dateMeetings);
				//Util.BIZ_CONF_DEBUG(TAG, "new a meeting: date:" + date);
			}
			
		};

		if (cursor != null) {

			cursor.close();
		}

		mMeetingDb.close();		
		//Util.BIZ_CONF_DEBUG(TAG, "load meeting number: " + dateToMeeting.size());
	}
}
